package com.job.client.message.listener;

import com.alibaba.fastjson2.JSON;
import com.job.client.common.result.ResponseResult;
import com.job.client.message.constant.MessageConstant;
import com.job.client.message.pojo.ConnectItem;
import com.job.client.message.pojo.Message;
import com.job.client.message.pojo.MessageItem;
import com.job.client.message.service.MessageService;
import com.job.client.model.message.constant.ChatLogRabbitmqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.data.mongodb.core.query.Query;
import com.job.client.common.utils.DateTimeUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
@Slf4j
public class MessageListener {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MessageService messageService;
    @RabbitListener(queues = ChatLogRabbitmqConstant.QUEUE_CHAT_LOG_MESSAGE)
    public void saveMessage(String dataJSON){
        Map<String, Objects> data = JSON.parseObject(dataJSON, Map.class);
        MessageItem messageItem = JSON.parseObject(JSON.toJSONString(data.get("messageItem")), MessageItem.class);
        String messageId = JSON.parseObject(JSON.toJSONString(data.get("messageId")), String.class);
        String connectId = JSON.parseObject(JSON.toJSONString(data.get("connectId")), String.class);

        /*
         * 保存自己的消息
         * */
        Query queryMy = new Query(Criteria.where("user_id").is(messageId)
                .and("connect.user_id").is(connectId));
        messageItem.setTime(DateTimeUtils.createNowTime());
        Update updateMy = new Update().push("connect.$.message", messageItem);  //插入到connect中的message，们想要在匹配的数组元素中执行此操作
        if(MessageConstant.BOSS_CONNECT_ID.equals(connectId) && MessageConstant.CONTENT_OTHER_STATUS.equals(messageItem.getStatus())){
            Integer number = 0;
            Message message = mongoTemplate.findOne(queryMy, Message.class);
            List<ConnectItem> connect = message.getConnect();
            for (ConnectItem connectItem : connect) {
                if(connectItem.getUserId().equals(connectId)){
                    number = connectItem.getNumber();
                }
            }
            updateMy.set("connect.$.number",number+1);
        }
        mongoTemplate.findAndModify(queryMy, updateMy, Message.class);
        /*
         * 保存别人的消息
         * */
        if(!MessageConstant.BOSS_CONNECT_ID.equals(connectId)){
            Query queryOt = new Query(Criteria.where("user_id").is(connectId)
                    .and("connect.user_id").is(messageId));
            Message message = mongoTemplate.findOne(queryOt, Message.class);
            List<ConnectItem> connect = message.getConnect();
            Integer number = 0;
            for (ConnectItem connectItem : connect) {
                if (connectItem.getUserId().equals(messageId)){
                    number = connectItem.getNumber();
                }
            }

            Update updateOt = new Update()
                    .push("connect.$.message", messageItem)
                    .set("connect.$.number",number+1);  //插入到connect中的message，们想要在匹配的数组元素中执行此操作
            mongoTemplate.findAndModify(queryOt, updateOt, Message.class);
        }
        // 发送webSocket
        if(MessageConstant.BOSS_CONNECT_ID.equals(connectId) && MessageConstant.CONTENT_MYSELF_STATUS.equals(messageItem.getStatus())){
            connectId = "231213002";
        }
        if(connectId.equals("1")){
            String tmp = connectId;
            connectId = messageId;
            messageId = tmp;
        }
            messageItem.setTime(DateTimeUtils.createNowTime());
            messageItem.setStatus(MessageConstant.CONTENT_OTHER_STATUS);
            Map<String,Object> webSocketData = new HashMap<>();
            webSocketData.put("messageItem",messageItem);
            webSocketData.put("userId",messageId);
            messageService.pushMessage(connectId,JSON.toJSONString(webSocketData));

    }
}
